package rpc

/*
import (
	"common/p2p"
	"context"
	"fmt"
	"net"
	"storj.io/drpc/drpcconn"
	"time"

	"errors"
)

type p2pDialer struct {
	p2pTimeout int
	devId      string
	localPort  uint16
	conn       *drpcconn.Conn
}

//func NewP2PDialer(timeoutMs int, devId string, port uint16) *P2PDialer {
//	return &P2PDialer{
//		p2pTimeout: timeoutMs,
//		devId:      devId,
//		port:       port,
//	}
//}

func DialNode(ctx context.Context, timeoutMs int, devId string, port uint16) (*p2pDialer, error) {
	if port == 0 {
		return nil, errors.New("error port")
	}

	d := &p2pDialer{
		p2pTimeout: 0,
		devId:      "",
		localPort:  0,
		conn:       nil,
	}

	t1 := time.Now()
	localPort, err := p2p.AddConnectRetry(devId, port, timeoutMs)
	fmt.Println(devId, " p2p use time", time.Now().Sub(t1).Seconds())
	if err != nil {
		return nil, err
	}
	d.localPort = localPort
	deadline, _ := ctx.Deadline()

	addr := fmt.Sprintf("127.0.0.1:%d", localPort)
	dialer := net.Dialer{
		Timeout:       0,
		Deadline:      deadline,
		LocalAddr:     nil,
		DualStack:     false,
		FallbackDelay: 0,
		KeepAlive:     0,
		Resolver:      nil,
		Cancel:        nil,
		Control:       nil,
	}
	c, err := dialer.DialContext(ctx, "tcp", addr)
	if err != nil {
		return nil, err
	}
	d.conn = drpcconn.New(c)
	return d, nil
}
func (d *p2pDialer) GetConn() *drpcconn.Conn {
	return d.conn
}
func (d *p2pDialer) Close() {
	if d.localPort > 0 {
		p2p.RemoveConnect(d.devId, d.localPort)
	}

	if d.conn != nil {
		d.conn.Close()
	}

}
*/
